﻿Imports SistemaINCA.Models.ViewModels
Imports SistemaINCA.Models.Providers

Namespace Controllers
    Public Class AccountsController
        Inherits System.Web.Mvc.Controller

        Public Property MembershipService As SchoolClickMemberShipProvider
        Public Property AuthorizationRolService As SchoolClickRoleProvider

        Protected Overrides Sub Initialize(requestContext As RequestContext)
            If (MembershipService Is Nothing) Then
                MembershipService = New SchoolClickMemberShipProvider
            End If

            If AuthorizationRolService Is Nothing Then
                AuthorizationRolService = New SchoolClickRoleProvider
            End If

            MyBase.Initialize(requestContext)
        End Sub

        <AllowAnonymous()> _
        Function LogOn() As ActionResult
            Return View()
        End Function

        <AllowAnonymous()> _
        <HttpPost()> _
        Function LogOn(Model As LoginModel, returnUrl As String) As ActionResult
            'Comprobamos que todos los datos sean validos
            If ModelState.IsValid Then
                Try
                    If MembershipService.ValidateUser(Model.UserName, Model.Password) Then
                        FormsAuthentication.SetAuthCookie(Model.UserName, False)

                        If Url.IsLocalUrl(returnUrl) Then Return Redirect(returnUrl) Else Return RedirectToAction("index", "home")
                    Else
                        ModelState.AddModelError("ErrorAuth", My.Resources.ErrorMessageResources.Error_UserANDPass_Model)
                    End If
                Catch ex As ApplicationException
                    ModelState.AddModelError("ErrorConnection", ex.Message)
                Catch ex As Exception
                    ModelState.AddModelError("ErrorEx", My.Resources.ErrorMessageResources.Error_ThrowException)
                End Try
            End If
            Return View()
        End Function

        <AllowAnonymous()> _
        <HttpGet()> _
        Function PasswordEncryptation() As ActionResult
            Dim enPWD As New EncryptationsModel With {.Rol_ERA = EncryptationSchoolClick.EncriptarData(EncryptationSchoolClick.ToStringSeguro("ReGIsTRarADaCad")),
                                                      .Rol_PRF = EncryptationSchoolClick.EncriptarData(EncryptationSchoolClick.ToStringSeguro("PrOFEnSeÑarCLaS")),
                                                      .ReportServer_Password = EncryptationSchoolClick.EncriptarData(EncryptationSchoolClick.ToStringSeguro("Developers2012")),
                                                      .CorreoEPassword = EncryptationSchoolClick.EncriptarData(EncryptationSchoolClick.ToStringSeguro("developers"))}
            Return View(enPWD)
        End Function
    End Class
End Namespace
